package org.dizena.common.interaction.response;

import lombok.Data;
import org.dizena.common.base.enums.EnumHeaderKey;
import org.dizena.common.base.enums.EnumResultCode;
import org.dizena.common.utils.ReqUtil;

@Data
public class ResultVO<T> {

    private int code;

    private String msg;

    private String trace;

    private T data;

    public ResultVO(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.trace = getTrace();
    }

    public ResultVO(EnumResultCode rc) {
        this.code = rc.code();
        this.msg = rc.msg();
        this.trace = getTrace();
    }

    public static ResultVO success() {
        ResultVO vo = new ResultVO(EnumResultCode.SUCCESS);
        return vo;
    }

    public static ResultVO success(Object data) {
        ResultVO vo = new ResultVO(EnumResultCode.SUCCESS);
        vo.setData(data);
        return vo;
    }

    public static ResultVO fail(int code, String msg) {
        ResultVO vo = new ResultVO(code, msg);
        return vo;
    }

    public static ResultVO fail(int code, String msg, Object data) {
        ResultVO vo = new ResultVO(code, msg);
        vo.setData(data);
        return vo;
    }

    public String getTrace() {
        return ReqUtil.getTrace(EnumHeaderKey.TRACE_ID.getKey());
    }

}
